/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package src.view.system;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import org.primefaces.context.RequestContext;
import org.primefaces.event.CloseEvent;
import src.domain.system.Branch;
import src.domain.system.BranchPc;
import src.services.system.BranchModel;
import src.util.Messager;

/**
 *
 * @author jay
 */
@ManagedBean
@SessionScoped
public class BranchBean implements Serializable {

    private List<Branch> branchs;
    private Branch selectedData, insertedData;
    private BranchPc unsaveIP;
    
    private BranchModel helper;
    private boolean editMode = true, editDetailMode=true;
    private int idx;
    
    public BranchBean() {
        this.helper = new BranchModel();
        this.branchs = helper.findAll(null);
        this.insertedData = null;
        this.selectedData = null;
    }

    public boolean isEditMode() {
        return editMode;
    }
        
    public List<Branch> getBranchs() {
        return this.branchs;
    }
    
    public Branch getSelectedData() {
        return selectedData;
    }

    public void setSelectedData(Branch selectedData) {
        this.selectedData = selectedData;
    }
    
    public Branch getInsertedData() {
        return insertedData;
    }

    public void setInsertedData(Branch insertedData) {
        this.insertedData = insertedData;
    }

    public BranchPc getUnsaveIP() {
        return unsaveIP;
    }

    public void setUnsaveIP(BranchPc unsaveIP) {
        this.unsaveIP = unsaveIP;
    }
    
    private void removeEmptyRow(){
        for(int i = 0; i<insertedData.getBranchPcs().size(); i++){
            if(insertedData.getBranchPcs().get(i).getIpAddress() == null) {
                insertedData.getBranchPcs().remove(i);
                i--;
            }
        }
    }
    
    public void handleRemoveRow() {  
        if (insertedData.getBranchPcs().size() == 1) insertedData.getBranchPcs().add(new BranchPc());
    }
    
    public void newDetail() {
        this.unsaveIP = new BranchPc();
        editDetailMode = false;
    }
    
    public void editDetail(BranchPc data) {
        this.idx = insertedData.getBranchPcs().indexOf(data);
        this.unsaveIP = BranchPc.clone(data);
        editDetailMode = true;
    }
    
    public void saveDetail() {
        boolean success= !isIPExistOnDB(unsaveIP.getIpAddress());
        
        if (success) {
            try {
                if(!editDetailMode) {
                    this.selectedData.getBranchPcs().add(unsaveIP);
                    removeEmptyRow();

                } else insertedData.getBranchPcs().set(idx, BranchPc.clone(unsaveIP));

            } catch (RuntimeException e) {success=false;
            } finally {Messager.showSaveUpdateStatus(success);}
        }
    }
    
    public void handleClose(CloseEvent event) {
        this.selectedData = null;
        this.insertedData = null;
        this.editMode = true;      
    }
    
    public void newData() {  
        this.selectedData = null;
        this.editMode = false;
        this.insertedData = new Branch();
        this.insertedData.getBranchPcs().add(new BranchPc());
    }
    
    public void openData() {
        this.editMode = true;
        this.insertedData = Branch.clone(selectedData);
        if (insertedData.getBranchPcs().isEmpty()) 
            this.insertedData.getBranchPcs().add(new BranchPc());
    }
    
    private boolean isIPExistOnDB(String ip) {
        boolean productExist;
        productExist = helper.isIPExist(ip, insertedData.getBranchId());
        if (productExist==true) Messager.showError("IPIsExist");
        return productExist;
    }
    
    private void removeDuplicateIP() {
        List<BranchPc> copy = new ArrayList<>();
        
        for (int i=0; i<insertedData.getBranchPcs().size(); i++) {
            copy.add(insertedData.getBranchPcs().get(i));
        }
        insertedData.setBranchPcs(copy);
    }
    
    private boolean validate() {
        boolean hasil;
        boolean productExist;
        
        removeEmptyRow();
        //removeDuplicateIP();
        
        productExist = helper.isBranchExist(insertedData.getBranchNo(), insertedData.getBranchId());
        if (productExist==true) {
            Messager.showError("codeIsExist");
            hasil = false;
        
        }else {
            int i=0;
            boolean found=false;
            while (i<insertedData.getBranchPcs().size() && !found) {
                found = isIPExistOnDB(insertedData.getBranchPcs().get(i).getIpAddress());
                i++;
            }
            hasil=!found;
        }
        
        if (hasil==false)
            if (insertedData.getBranchPcs().isEmpty()) this.insertedData.getBranchPcs().add(new BranchPc());
        
        return hasil;
    }
    
    public void trySave() {
        if(this.validate()) save();
    }
    
    private void save() {
        RequestContext context = RequestContext.getCurrentInstance();
                
        boolean success = helper.saveData(insertedData);
        if (success==true) {
            if (this.editMode==false) this.branchs.add(insertedData);
            else {
                int i = branchs.indexOf(selectedData);
                if (i <= branchs.size()) branchs.set(i, Branch.clone(insertedData));
            }
            editMode = true;
            
        } else {
            if (this.editMode==false) insertedData.setBranchId(null);
        }
        
        Messager.showSaveUpdateStatus(success);        
        context.addCallbackParam("success", success);  
    }
    
    public void delete() {
        RequestContext context = RequestContext.getCurrentInstance();  
        boolean success = helper.deleteData(this.selectedData);
        
        if (success) {
            this.branchs.remove(this.selectedData);
            this.selectedData = null;
        } 
        Messager.showDeleteStatus(success);  
        context.addCallbackParam("success", success);  
    }
    
}
